`
https://leetcode.cn/problems/find-all-anagrams-in-a-string/
`

/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
var findAnagrams = function (s, p) {
  const needs = {}
  const window = {}

  for (const c of p) {
    needs[c] = (needs[c] || 0) + 1
  }

  let left = 0, right = 0
  let count = 0

  const result = []

  while (right < s.length) {
    const c = s[right]
    right++

    if (needs[c]) {
      window[c] = (window[c] || 0) + 1
      if (window[c] === needs[c]) {
        count++
      }
    }

    while (right - left >= p.length) {
      if (count === Object.keys(needs).length) {
        result.push(left)
      }

      const d = s[left]
      left++

      if (needs[d]) {
        if (needs[d] === window[d]) {
          count--
        }
        window[d]--
      }
    }
  }
  return result
};